import * as API from "@/services";
import { createUserAuth } from '@/services/system/auth/AuthService';
import { Button, Form, Input, message, Spin } from 'antd';
import {KfFloatingContextProps,DEFAULT_FORM_LAYOUT, DEFAULT_FORM_TAIL_LAYOUT} from "kf-web-library";
import React, { useState } from 'react';

type SubmitEntity = API.Auth_Create;
const FormItem = Form.Item;

interface CreateAuthFormProps extends KfFloatingContextProps {
    userId: API.EntityId;
}

const CreateAuthForm: React.FC<CreateAuthFormProps> = (props) => {
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();

    return (
        <Spin spinning={loading} tip="正在加载数据,请稍候..." delay={500}>
            <Form
                {...DEFAULT_FORM_LAYOUT}
                form={form}
                onFinish={(values) => {
                    setLoading(true);
                    createUserAuth({ ...values, userId: props.userId } as API.Auth_Create)
                        .then((res) => {
                            if (res.flag) {
                                message.success('新增成功');
                                props.closeWrapFloating && props.closeWrapFloating(true);
                            } else {
                                setLoading(false);
                            }
                        })
                        .finally(() => {
                            setLoading(false);
                        });
                }}
            >
                <FormItem name={nameof<SubmitEntity>((x) => x.identifier)} label="登录账号" rules={[{ required: true }]}>
                    <Input placeholder={'用户名/手机号/邮箱'} />
                </FormItem>

                <FormItem name={nameof<SubmitEntity>((x) => x.credential)} label="登录密码" rules={[{ required: true }]}>
                    <Input.Password />
                </FormItem>

                <Form.Item {...DEFAULT_FORM_TAIL_LAYOUT}>
                    <Button type="primary" htmlType="submit" disabled={loading}>
                        确认新增
                    </Button>
                </Form.Item>
            </Form>
        </Spin>
    );
};

export default CreateAuthForm;
